#include "precompile.h"
#include "NebulaMemory.h"


bool NebulaMemory::ms_heapCreated = false;
HANDLE NebulaMemory::ms_heap = 0;

NebulaMemory::NebulaMemory(void)
{
}

NebulaMemory::~NebulaMemory(void)
{
}

void NebulaMemory::SetUp()
{
	const size_t initialsize = 4*1024*1024;
	ms_heap = ::HeapCreate(0, initialsize, 0);

	ULONG heapFragValue = 2;
	HeapSetInformation(ms_heap, HeapCompatibilityInformation, &heapFragValue, sizeof(heapFragValue));
}

void NebulaMemory::Exit()
{

}

void* NebulaMemory::Alloc(size_t size)
{
	if(!ms_heapCreated)
	{
		NebulaMemory::SetUp();
		ms_heapCreated = true;
	}

	assert(ms_heap != NULL);
	void* ptr = __HeapAlloc16(ms_heap, 0, size);
	assert(ptr != NULL);

	return ptr;
}

void NebulaMemory::Free(void* p)
{
	assert(ms_heap != NULL);
	__HeapFree16(ms_heap, 0, p);
}
